package com.wxtbk.scheduler.task;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.*;
import com.jfinal.aop.Duang;
import com.jfinal.log.Log;
import com.wxtbk.model.*;
import com.wxtbk.service.ItemService;

public class HaodankuTask implements Runnable {

	private Log log = Log.getLog(HaodankuTask.class);

	@Override
	public void run() {
		String url = "http://www.haodanku.com/index/index/nav/3/starttime/7/p/";
		Boolean isLast = false;
		int i = 0;
		while(!isLast && i< 1){
			 URL u;
			try {
				u = new URL(url + (++i) + ".html?json=true");
	            URLConnection connection = u.openConnection();
	            HttpURLConnection htCon = (HttpURLConnection) connection;
	            int code = htCon.getResponseCode();
	            if (code == HttpURLConnection.HTTP_OK){
	            	
	        		//File f = new File("f:\\hdk.json");
	        		//InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");//考虑到编码格式
	        		//BufferedReader in=new BufferedReader(read);
	        		
	            	BufferedReader in=new BufferedReader(new InputStreamReader(htCon.getInputStream(),"UTF-8"));
	        		
	        		        	
	                String inputLine = null;
	                
	                while ((inputLine = in.readLine()) != null) {
	                	
	                	Duang.duang(ItemService.class).SaveHaodankuItem(inputLine);
	   

	                }
	            			                
                    in.close();	            	
	            }else{
	            	log.debug("http status:" + code);
	            	isLast = true;
	            }
			}catch (Exception e){
				log.error(e.getMessage(),e);
			}
			finally{
				/*
				 try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
				log.info("成功采集haodanku的页数:"+ i);
			}

		}log.info("采集haodanku縂頁數"+i);
}

}
